<template>
  <el-card class="layout-query">
    <el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item prop="gnssCenterId">
        <el-input v-model="state.queryForm.gnssCenterId" placeholder="平台ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <el-card>
    <el-space>
      <el-space>
        <el-button v-auth="'gps:jt809_user:save'" icon="Plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-space>
      <!--      <el-space>-->
      <!--        <el-button-->
      <!--          v-auth="'gps:jt809_user:delete'"-->
      <!--          icon="Delete"-->
      <!--          plain-->
      <!--          type="danger"-->
      <!--          @click="deleteBatchHandle()"-->
      <!--          >批量删除</el-button-->
      <!--        >-->
      <!--      </el-space>-->
    </el-space>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="gnssCenterId" label="平台ID" header-align="center" align="center"></el-table-column>
      <el-table-column prop="protocol" label="协议号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="userType" label="类型" width="200" header-align="center" align="center">
        <template #default="scope">
          <div v-if="scope.row.userType == 'UP_USER'"><el-tag type="danger">我是下级</el-tag>-><el-tag type="primary">上级平台</el-tag></div>
          <div v-else><el-tag type="warning">下级平台</el-tag>-><el-tag type="danger">我是上级</el-tag></div>
        </template>
      </el-table-column>
      <el-table-column label="上级平台地址" header-align="center" align="center">
        <template #default="scope">
          <span>{{ scope.row.platformIp ? scope.row.platformIp : '忽略' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="platformPort" label="上级平台端口" header-align="center" align="center"></el-table-column>
      <el-table-column prop="userId" label="用户名" header-align="center" align="center"></el-table-column>
      <el-table-column prop="password" label="密码" header-align="center" align="center"></el-table-column>
      <el-table-column prop="password" width="200" label="连接" header-align="center" align="center">
        <template #default="scope">
          <el-space spacer="|">
            <el-tag type="info">未连接</el-tag>
            <el-check-tag v-if="scope.row.userType == 'UP_USER'" type="primary" checked @click="connect1(scope.row.gnssCenterId, true)">连接</el-check-tag>
          </el-space>
        </template>
      </el-table-column>
      <el-table-column prop="enrypt" label="是否加密" header-align="center" align="center">
        <template #default="scope">
          <span>{{ scope.row.enrypt ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="encryptKey" label="秘钥" header-align="center" align="center"></el-table-column>
      <el-table-column prop="miyaoM" label="M1" header-align="center" align="center"></el-table-column>
      <el-table-column prop="miyaoA" label="A1" header-align="center" align="center"></el-table-column>
      <el-table-column prop="miyaoC" label="C1" header-align="center" align="center"></el-table-column>
      <el-table-column prop="connSubLink" label="子链路连接" header-align="center" align="center">
        <template #default="scope">
          <span>{{ scope.row.enrypt ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="300">
        <template #default="scope">
          <el-button v-auth="'gps:jt809_user:update'" type="primary" link @click="addOrUpdateHandle(scope.row.gnssCenterId)">修改</el-button>
          <el-button v-auth="'gps:jt809_user:delete'" type="primary" link @click="deleteBatchHandle(scope.row.gnssCenterId)">删除</el-button>
          <el-row>
            <el-col v-for="(item, index) in scope.row.userType == 'UP_USER' ? 4 : 3" :key="index" :span="6">
              <el-button v-auth="'gps:jt809_user:delete'" type="success" link @click="logListHandle(scope.row.gnssCenterId)">日志列表{{ item }}</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.page"
      :page-sizes="state.pageSizes"
      :page-size="state.limit"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>
    <log-list ref="logListRef"></log-list>
  </el-card>
</template>

<script setup lang="ts" name="GpsJt809_userIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './add-or-update.vue'
import LogList from '@/views/gps/jt809_user/log-list.vue'
import { connectApi } from '@/api/gps/jt809'

const state: IHooksOptions = reactive({
  dataListUrl: '/gps/jt809_user/page',
  deleteUrl: '/gps/jt809_user',
  queryForm: {
    gnssCenterId: ''
  }
})

//连接
function connect1(gnssId, processMainConn) {
  connectApi(gnssId, processMainConn)
}

const queryRef = ref()
const addOrUpdateRef = ref()
const logListRef = ref()
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id)
}
const logListHandle = (id?: number) => {
  logListRef.value.init(id)
}
const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle, reset } = useCrud(state)
</script>
